package com.tansuo365.test1.controller.othercontrol;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tansuo365.test1.bean.otherbean.HuiShouDetails;
import com.tansuo365.test1.bean.otherbean.HuiShouZong;
import com.tansuo365.test1.service.otherservice.HuishouDetailsService;
import com.tansuo365.test1.service.otherservice.HuishouZhixingService;
import com.tansuo365.test1.service.otherservice.HuishouZongService;
import com.tansuo365.test1.util.DateUtil;
import com.tansuo365.test1.util.StringUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@PropertySource(value = "classpath:danhao.properties")
@Api(value = "回收单相关控制层", tags = "", description = "")
@RequestMapping("/admin/huishoudan")
@RestController
public class HuishouDanController {

    @Autowired
    private HuishouZongService huishouZongService;
    @Autowired
    private HuishouDetailsService huishouDetailsService;
    @Autowired
    private HuishouZhixingService huishouZhixingService;

    @Value("${danhao.huishoudan.prefix}")
    private String huishoudanPrefix;


    /**
     * 获取回收单号 OK
     *
     * @param type
     * @return
     * @throws Exception
     */
    @Async
    @RequestMapping("/getHuishouNumber")
    public ListenableFuture<String> genBillCode(String type) throws Exception {
        StringBuffer billCodeStr = new StringBuffer();
        billCodeStr.append(huishoudanPrefix);
        billCodeStr.append(DateUtil.getCurrentDateStr());
        String todayMaxShengouDanNumber = huishouZongService.getTodayMaxHuiShouDanNumber();
        if (todayMaxShengouDanNumber != null) {
            billCodeStr.append(StringUtil.formatCode(todayMaxShengouDanNumber));
        } else {
            billCodeStr.append("0001");
        }
        return new AsyncResult<>(billCodeStr.toString());
    }

    @RequestMapping("/getSelective")
    public Map<String, Object> getHuiShouDan(HuiShouZong huiShouZong, @RequestParam(value = "page", required = true) Integer page, @RequestParam(value = "rows", required = true) Integer rows) {
        if (huiShouZong == null) {
            return null;
        }
        System.out.println("项目名称:" + huiShouZong.getXiangmumingcheng());
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(page, rows);
        List<HuiShouZong> huishouZongsList = huishouZongService.listHuiShouZong(huiShouZong);
        PageInfo<HuiShouZong> pageInfo = new PageInfo<HuiShouZong>(huishouZongsList);
        resultMap.put("rows", pageInfo.getList());
        resultMap.put("total", pageInfo.getTotal());
        return resultMap;
    }

    /**
     * 根据zongzhang总表的id查询对应的子表数据 分页
     *
     * @param page
     * @param rows
     * @return 根据总表id遍历查询详细的物料集合
     */
    @RequestMapping("/listDetailsByZId")
    public Map<String, Object> listDetailsByZongId(HuiShouDetails huishouDetails, @RequestParam(value = "page", required = true) Integer page, @RequestParam(value = "rows", required = true) Integer rows) {
        if (huishouDetails == null) {
            return null;
        }
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(page, rows);
        List<HuiShouDetails> huishouDetailsList = huishouDetailsService.listHuiShouDetailsSelective(huishouDetails);
        PageInfo<HuiShouDetails> pageInfo = new PageInfo<HuiShouDetails>(huishouDetailsList);
        resultMap.put("rows", pageInfo.getList());
        resultMap.put("total", pageInfo.getTotal());
        return resultMap;
    }

    /**
     * (回收单的总和子表)
     * 保存申购总表+子表数据 OK
     *
     * @return
     */
    //1.到货单保存后,同时写入申购数量,到货数量到申购执行汇总
    //2.到货单保存后,同时写入库存查询表,申购量/到货量,进行计算的(未到货量)
    @RequestMapping("/save")
    public Map<String, Object> save(HttpSession session, HuiShouZong huishouZong, @RequestParam("wuliaoHuishouDetailsJson") String wuliaoHuishouDetailsJson) {
        if (huishouZong == null || wuliaoHuishouDetailsJson == null) {
            return null;
        }
        Map<String, Object> resultMap = new HashMap<>();
        Gson gson = new Gson();
        List<HuiShouDetails> huishouDetailsList = gson.fromJson(wuliaoHuishouDetailsJson, new TypeToken<List<HuiShouDetails>>() {
        }.getType());

        huishouZong.setFounder((String) session.getAttribute("truename"));
        //save时,先保存大表,大表返回id后再保存附表
        int insertCode = huishouZongService.saveZongAndDetails(huishouZong, huishouDetailsList);
        //save时,保存到货单数据到申购执行,选择其中huishouDetailsList中shengoudanhao不为空的进行保存转换
        int zhixingSaveCode = huishouZhixingService.saveHuiShouBindChuku(huishouDetailsList, huishouZong);
        if (insertCode > 0 && zhixingSaveCode == 0) {
            resultMap.put("success", true);
            resultMap.put("msg", "保存成功.");
//            wuliaoKucunSearService.
        } else if (insertCode > 0 && zhixingSaveCode > 0) {
            resultMap.put("success", true);
            resultMap.put("msg", "保存成功!!");
        } else {
            resultMap.put("success", false);
            resultMap.put("errorInfo", "保存失败");
        }
        return resultMap;
    }

    /**
     * @return
     */
    @RequestMapping("/update")
    public Map<String, Object> update(HuiShouZong huishouZong) {
        if (huishouZong == null) {
            return null;
        }
        Map<String, Object> resultMap = new HashMap<>();
        int updateCode = huishouZongService.updateByPrimaryKeySelective(huishouZong);
        if (updateCode > 0) {
            resultMap.put("success", true);
        } else {
            resultMap.put("success", false);
        }
        return resultMap;
    }

    /**
     * 删除  这里的删除同样删除了子表数据
     *
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    @Transactional
    public Map<String, Object> deleteBatch(@RequestParam("ids[]") Integer[] ids) {
        Map<String, Object> resultMap = new HashMap<>();
        if (ids == null || ids.length == 0) {
            resultMap.put("success", false);
            resultMap.put("errorInfo", "请选中条目进行删除");
            return resultMap;
        }
//        int changeCode = huishouZhixingService.deleteTheHuishouFromIdArr(ids);  //批量更改(-)回收数量和未到货
        int delBindCode = huishouZhixingService.deleteHuishouBindChuku(ids);//删除回收执行中回收数据的数值(update-少)
        int code = huishouZongService.deleteBatchByPKArr(ids);  //批量删除回收主表
        int ziDelCode = 0;
        if (code > 0) {
            ziDelCode = huishouDetailsService.deleteByZongIdArr(ids); //批量删除回收详细根据ids条目
        }
        //删除回收汇总中的对应这些id的回收量,作为-去回收量来计算
        if (code > 0 && ziDelCode > 0 && delBindCode > 0) {
            resultMap.put("success", true);
            return resultMap;
        } else {
            resultMap.put("success", false);
            return resultMap;
        }
    }
}
